package game.Actors;

import java.util.ArrayList;

import game.LevelManager;
import game.NodeGraph;
import game.Config.Config;

import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;
/**
 * This class represents an enemy on the screen.
 * When created, the enemy will spawn on a startnode of a given
 * graph, and will then move through the graph node to node.
 * When reaching the end node or being killed, the enemy will 
 * remove itself.
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class Enemy extends MovableActor{
	private static ArrayList<Enemy> enemies = new ArrayList<Enemy>();
	private Vector2f target;
	private NodeGraph path;
	private float maxHealth;
	private float currentHealth;
	private HealthBar healthBar;
	private SpriteSheet deathAnimation;
	protected float originalSpeed;
	
	/**
	 * Constructor for creating enemies.
	 * Creates the enemy with given parameters.
	 * @param image The image we want to use for the enemy.
	 * @param path The path the enemy is going to traverse.
	 * @param speed The speed of the enemy.
	 * @param health The maximum health of the enemy.
	 */
	public Enemy(Image image,NodeGraph path,float speed, float health,SpriteSheet deathAnimation) {
		super(image, path.getNextNodePosition().copy());
		this.path = path;
		this.target = path.getNextNodePosition().copy();
		this.speed = speed;
		this.originalSpeed = speed;
		this.currentHealth = health;
		this.maxHealth = health;
		this.setDirection(this.target.copy().add(new Vector2f(-this.position.x,-this.position.y)));
		this.setImageRotation();
		this.healthBar = new HealthBar(this.position);
		this.deathAnimation = deathAnimation;
		enemies.add(this);
	}
	/**
	 * Updates the enemy position, making it move in the direction of the target node.
	 * Also updates the position of the healthbar associated with this enemy. 
	 */
	@Override
	public void update(int delta){
		if(this.position.copy().add(new Vector2f(-target.x,-target.y)).length() <= Config.ENEMY_PROXIMITY_RANGE){
			if(path.hasNext()){
				this.target = path.getNextNodePosition().copy();
			} else {
				this.reachedGoal();
			}
			this.setDirection(this.target.copy().add(new Vector2f(-this.position.x,-this.position.y)));
			this.setImageRotation();
		}
		super.update(delta);
		this.healthBar.update(position);	
	}
	/**
	 * Overrides Actors draw, also drawing the enemies healthbar.
	 */
	@Override
	protected void draw(){
		super.draw();
		this.healthBar.draw();

	}
	
	/**
	 * Is called when the bullet hits an enemy.
	 * This class adjusts the enemy health
	 * and furthermore adjusts the healthbar accordingly.
	 * If the enemy is killed, it will die and drop money.
	 */
	public void hit(float damage) {
		this.currentHealth -= damage;
		if(this.currentHealth <= 0) {
			this.die();
			this.dropMoney();
		}
		this.healthBar.setPercentage(currentHealth/maxHealth);
	}

	private void dropMoney(){
		new Money(Config.moneyImage,this.position.copy(),(int)this.maxHealth*Config.MONEY_MULTIPLIER);
	}
	private void reachedGoal(){
		LevelManager.destroyEgg();
		this.isDead = true;
	}
	private void die() {
		this.isDead = true;
		new AnimationActor(this.position.copy(),this.deathAnimation);
	}
	@Override
	protected void remove(){
		super.remove();
		enemies.remove(this);
	}
	/**
	 * @return	A list of all the enemies in the game.
	 */
	
	public static ArrayList<Enemy> getEnemyList() {
		return enemies;
	}
}
